﻿using ZeroSum.Constants;
using ZeroSum.DependencyInjection.Attributes;
using ZeroSum.Domain.Repositories;
using ZeroSumZSA.Core.Aggregates.MenuAggregate;
using ZeroSumZSA.Core.Aggregates.RoleAggregate;

namespace ZeroSumZSA.Core.DomainServices.Roles;

[Register]
public class RoleDataSeedProvider : IDataSeedProvider
{
    private readonly IRepository<Menu> _menuRepository;
    private readonly IRepository<Role> _repository;

    public RoleDataSeedProvider(IRepository<Role> repository, IRepository<Menu> menuRepository)
    {
        _repository = repository;
        _menuRepository = menuRepository;
    }

    public async Task GenerateAsync()
    {
        if (await _repository.CountAsync() <= 0)
        {
            var menus = await _menuRepository.GetListAsync(QueryByEntity<Menu>.Of());

            var roles = new List<Role>
            {
                new()
                {
                    Id = 142307070910554,
                    Name = "系统管理员",
                    Code = "sys_manager_role",
                    NormalizeCode = "sys_manager_role",
                    Sort = 100,
                    Remark = "系统管理员",
                    Menus = menus
                },
                new()
                {
                    Id = 142307070910555,
                    Name = "普通用户",
                    Code = "common_role",
                    NormalizeCode = "common_role",
                    Sort = 101,
                    Remark = "普通用户",
                    Menus = menus
                }
            };

            await _repository.AddRangeAsync(roles);
            await _repository.SaveChangesAsync();
        }
    }

    public int Order { get; set; } = 2;
}